package com.quectel.core.module.dept.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.dept.dao.DeptDao;
import com.quectel.core.module.dept.dto.DeptDto;
import com.quectel.core.module.dept.entity.DeptEntity;
import com.quectel.core.module.dept.service.DeptService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/03/24 10:07
 */
@DubboService
public class DeptServiceImpl implements DeptService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeptServiceImpl.class);

    private static final String DEPT_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "DEPT_CACHE_BY_ID:";

    @Autowired
    private DeptDao deptDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(DeptDto deptDto) {
        if (deptDto.getId() == null) {
            Long id = save(deptDto);
            deptDto.setId(id);
        } else {
            updateAllColumnById(deptDto);
        }

    }

    @Override
    public List<DeptDto> selectByParentId(Long id) {
        return null;
    }

    @Override
    public DeptDto selectByName(Long villageId, String name) {
        DeptEntity entity = deptDao.selectOne(new LambdaQueryWrapper<DeptEntity>()
                .eq(DeptEntity::getVillageId, villageId)
                .eq(DeptEntity::getName, name)
                .last(SqlConstants.MYSQL_LIMIT_ONE)
        );
        return CopyUtils.copyObj(entity, DeptDto.class);
    }

    @Override
    public DeptDto selectByCode(Long villageId, String code) {
        DeptEntity entity = deptDao.selectOne(new LambdaQueryWrapper<DeptEntity>()
                .eq(DeptEntity::getVillageId, villageId)
                .eq(DeptEntity::getName, code)
                .last(SqlConstants.MYSQL_LIMIT_ONE)
        );
        return CopyUtils.copyObj(entity, DeptDto.class);
    }

    @Override
    public List<DeptDto> queryByRecursion(Long deptId) {
        List<DeptEntity> list = deptDao.queryByRecursion(deptId);
        List<DeptDto> result = CopyUtils.copyList(list, DeptDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public DeptDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                DEPT_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public DeptDto selectById(Long id) {
        DeptEntity entity = deptDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, DeptDto.class));
    }

    @Override
    public List<DeptDto> queryList(Map<String, Object> params) {
        List<DeptEntity> list = deptDao.queryList(params);
        List<DeptDto> result = CopyUtils.copyList(list, DeptDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private DeptDto paddingField(DeptDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return deptDao.queryTotal(params);
    }

    @Override
    public Long save(DeptDto dto) {
        DeptEntity entity = CopyUtils.copyObj(dto, DeptEntity.class);
        entity.setId(Snowflake.nextId());
        deptDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(DeptDto dto) {
        DeptEntity entity = CopyUtils.copyObj(dto, DeptEntity.class);
        deptDao.updateById(entity);
        CacheKit.invalidRedisCache(DEPT_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(DeptDto dto) {
        DeptEntity entity = CopyUtils.copyObj(dto, DeptEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, deptDao);

        CacheKit.invalidRedisCache(DEPT_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        deptDao.deleteById(id);

        CacheKit.invalidRedisCache(DEPT_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
